//
//  DataFillViewController.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/9/1.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON

class DataFillViewController: MainViewController {
    var dataFillView:DataFillView!
    var itemSelect:ItemSelecteView!
//    var xmlModel:XMLModel!
    var menuModel:MenuModel!
    var itemCell:ItemSelecteCell!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "资料填写"
        self.getJsonAlamofire()
        self.operationToNaviagtionItem()
        self.addDataFillView()
    }
}

//MARK:--UI设置
extension DataFillViewController{
    
    //操作naviagtioncontroll  隐藏返回按钮
    func operationToNaviagtionItem(){
        self.navigationItem.leftBarButtonItem?.customView?.hidden = true
    }
    
    //添加datafillView进来
    func addDataFillView(){
        //表面视图
        dataFillView = DataFillView(frame: MainBounds)
        self.view.addSubview(dataFillView)
    }
    
    func addTableView(){
        //项目选择器视图
        itemSelect = ItemSelecteView(frame: CGRect(x: 0,y: WidthAPP-378*SCALEH,width: WidthAPP,height: 378*SCALEH))
        self.addTargrtForView()
    }
    
    //为控件添加点击事件 和 代理事件
    func addTargrtForView(){
        
        
        
        let beGoodAtTF = dataFillView.beGoodAtTF
        beGoodAtTF?.inputView = itemSelect
        beGoodAtTF?.delegate = self
        
        //为三个tableView添加delegate  和datasource
        let firstTable = itemSelect.firstFrameTable
        firstTable?.tag = 5431
        
        let secondTable = itemSelect.secondFrameTable
        secondTable?.tag = 5432
        
        let threeTable = itemSelect.threeFrameTable
        threeTable?.backgroundColor = UIColor.whiteColor()
        threeTable?.tag = 5433
        
        
        firstTable?.delegate = self
        firstTable?.dataSource = self
        secondTable?.delegate = self
        secondTable?.dataSource = self
        threeTable?.delegate = self
        threeTable?.dataSource = self
        
        firstTable?.selectRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), animated: true, scrollPosition: .None)
        secondTable?.selectRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), animated: true, scrollPosition: .None)
        threeTable?.selectRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), animated: true, scrollPosition: .None)
        
        
        //下一步按钮添加点击事件
        dataFillView.nextButton.addTarget(self, action: #selector(self.nextButtonClick(_:)), forControlEvents: .TouchUpInside)
    }
}


//MARk:--点击事件
extension DataFillViewController{
    //下一步按钮点击事件
    func nextButtonClick(sender:UIButton){
        self.navigationController?.pushViewController(Avatar_UserNameSetViewController(), animated: true)
    }
}

//MARk:--textField Delegate
extension DataFillViewController:UITextFieldDelegate{
    func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
        return true
    }
}

//MARK:--tableview  delegate  datasource
extension DataFillViewController:UITableViewDelegate,UITableViewDataSource{
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        menuModel.deleteMenu()
        menuModel.getMenuItemCount()
        switch tableView.tag {
        case 5431:
            return menuModel.count1
        case 5432:
            return menuModel.count2
        case 5433:
            return menuModel.count3
        default:
            return 0
        }
    }
    
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        itemCell = ItemSelecteCell(style: .Default, reuseIdentifier: "itemCell")
        itemCell.selectionStyle = .Blue
        let view = UIView(frame: CGRect(x: 0,y: 0,width: 125*SCALEW, height: 35*SCALEH))
        view.backgroundColor = UIColor.whiteColor()
        itemCell.selectedBackgroundView = view
        
        let itemLabel = itemCell.itemLabel
        switch tableView.tag {
        case 5431:
            let view = UIView(frame: CGRect(x: 0,y: 0,width: 125*SCALEW, height: 35*SCALEH))
            view.backgroundColor = UIColor.whiteColor()
            itemCell.selectedBackgroundView = view
            itemLabel?.text = menuModel.getMenuItem(1, row: indexPath.row)
        case 5432:
            itemLabel?.text = menuModel.getMenuItem(2, row: indexPath.row)
        case 5433:
            itemCell.selectionStyle = .None
            itemCell.backgroundColor = UIColor.whiteColor()
            itemLabel?.text = menuModel.getMenuItem(3, row: indexPath.row)
        default:
            break
        }
        
        return itemCell
    }
    
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        switch tableView.tag {
        case 5431:
            menuModel.selecte1 = indexPath.row
            menuModel.selecte2 = 0
            let secTable = itemSelect.secondFrameTable
            let thrTable = itemSelect.threeFrameTable
            secTable?.reloadData()
            thrTable?.reloadData()
            secTable?.selectRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), animated: true, scrollPosition: .None)
        case 5432:
            menuModel.selecte2 = indexPath.row
            let thrTable = itemSelect.threeFrameTable
            thrTable?.reloadData()
        case 5433:
            let itemString = menuModel.getMenuItem(3, row: indexPath.row)
            let itemID = menuModel.menuID3[indexPath.row]
            if let hud = self.view.window!.viewWithTag(999) as? WaitHUD{
                self.postMenuIDAlamofire(itemID,hud: hud)
            }
            let beGoodTF = dataFillView.beGoodAtTF
            if beGoodTF?.isFirstResponder() == true{
                dataFillView.addItemToBeGoodTF(itemString,mune_id: itemID)
                if dataFillView.itemX > (WidthAPP/3*2 - 30*SCALEW){
                    beGoodTF?.placeholder = ""
                }else{
                    beGoodTF?.textAlignment = .Right
                    beGoodTF?.placeholder = "双击可删除选项"
                }
            }
        default:
            break
        }

    }
}



//MARK:--Alamofire
extension DataFillViewController{
    //得到menu列表
    func getJsonAlamofire(){
        Alamofire.request(.POST, "http://srxapp.zhekd.com/ios.php/Menu").responseJSON { (data) in
            let json = JSON(data: data.data!).arrayValue
            self.menuModel = MenuModel(menu: json,selecte1: 0,selecte2: 0)
            dispatch_async(dispatch_get_main_queue(), {
                self.addTableView()
            })
        }
    }
    
    //给服务器传送menuId
    func postMenuIDAlamofire(menu_id:String,hud:WaitHUD){
        let parameters = ["menu_id":menu_id]
        let headers = ["token":OperateUserDefaults.takeUserInfo().token]
        Alamofire.request(.POST, "http://srxapp.zhekd.com/ios.php/Menu/in_menu", parameters: parameters, encoding: .JSON, headers: headers).responseJSON { (data) in
            let json = JSON(data: data.data!)
            let code = json["code"].stringValue
            let message = json["message"].stringValue
            print(message)
            if code == "1"{
                hud.alpha = 0
            }else{
                hud.alpha = 0
                showHUD.showTopHud(self.view, text: message)
            }

            
        }
    }

}














